package com.tanhua.server.service;

import cn.hutool.core.convert.Convert;
import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.api.SoulApi;
import com.tanhua.dubbo.server.pojo.*;
import com.tanhua.dubbo.server.pojo.Dimension;
import com.tanhua.server.vo.OptionVo;
import com.tanhua.server.vo.QuestionVo;
import com.tanhua.server.vo.ResultVo;
import com.tanhua.server.vo.SoulVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SoulService {

    @Reference(version = "1.0.0")
    private SoulApi soulApi;

    @Autowired
    private UserInfoService userInfoService;

    public List<SoulVo> getSoulTest() {
        User user = UserThreadLocal.get();
        try {
            List<Soul> soulList = soulApi.querySoul();

               List<SoulVo> list = new ArrayList<>();
                for (Soul soul : soulList) {
                    SoulVo soulVo = new SoulVo();
                    soulVo.setId(soul.getId().toString());
                    soulVo.setName(soul.getName());
                    soulVo.setCover(soul.getCover());
                    soulVo.setLevel(soul.getLevel());
                    soulVo.setStar(soul.getStar());
                    if("中级灵魂题".equals(soul.getName())){
                        Boolean bool = soulApi.querySoulTestByUser(user.getId().toString(), "初级灵魂题");
                        if(bool){
                            soulVo.setIsLock(0);
                        }
                    }
                    if("高级灵魂题".equals(soul.getName())){
                        Boolean bool = soulApi.querySoulTestByUser(user.getId().toString(), "中级灵魂题");
                        if(bool){
                            soulVo.setIsLock(0);
                        }
                    }
                    if("初级灵魂题".equals(soul.getName())){
                        soulVo.setIsLock(0);
                    }
                    List<SoulQuestion> soulQuestions = soulApi.querySoulQuestion(soul.getId());

                    List<QuestionVo> questionVos = new ArrayList<>();
                    for (SoulQuestion soulQuestion : soulQuestions) {
                        QuestionVo questionVo = new QuestionVo();
                        questionVo.setId(soulQuestion.getId().toString());
                        questionVo.setQuestion(soulQuestion.getQuestion());
                        List<Option> options = soulApi.queryOption(soulQuestion.getQid());

                        List<OptionVo> optionVos = new ArrayList<>();
                        for (Option option : options) {
                            OptionVo optionVo = new OptionVo();
                            optionVo.setId(option.getId().toString());
                            optionVo.setOption(option.getOption());
                            optionVos.add(optionVo);
                        }
                        questionVo.setOptions(optionVos);
                        questionVos.add(questionVo);
                    }
                    soulVo.setQuestions(questionVos);
                    list.add(soulVo);
                }
           return list;
        } catch (Exception e) {
            log.error("查询出错了~ userId = " + user.getId(),e);
        }
        return null;
    }

    public String getResult(Answer[] answers) {
        try {
            User user = UserThreadLocal.get();
            String result = soulApi.result(answers,user.getId().toString());
            if(result == null){
                return null;
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public ResultVo getTestTable(String id) {
        try {
            Integer score = soulApi.queryScoreById(id);
            Conclusion conclusion = soulApi.queryResultByScore(score);
            ResultVo resultVo = new ResultVo();
            resultVo.setConclusion(conclusion.getConclusion());
            resultVo.setCover(conclusion.getCover());
            List<Dimension> dimensionList = new ArrayList<>();
            dimensionList.add(new Dimension("外向","80%"));
            dimensionList.add(new Dimension("判断","70%"));
            dimensionList.add(new Dimension("抽象","90%"));
            dimensionList.add(new Dimension("理性","60%"));
            resultVo.setDimensions(dimensionList);
            User user = UserThreadLocal.get();
            List<SoulTime> soulTimes = soulApi.queryUserByUser(score,user.getId().toString());
            List<SimilarYou> similarYous = new ArrayList<>();
            for (SoulTime soulTime : soulTimes) {
                SimilarYou similarYou = new SimilarYou();
                UserInfo userInfo = userInfoService.queryUserInfoByUserId(Long.valueOf(soulTime.getUserId()));
                similarYou.setId(Convert.toInt(userInfo.getUserId()));
                similarYou.setAvatar(userInfo.getLogo());
                similarYous.add(similarYou);
            }
            resultVo.setSimilarYou(similarYous);
            return resultVo;
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return null;
    }
}
